<?php
namespace app\model;

use app\service\UUIDService;
use const support\CORRECT;

class MemberModel extends BaseModel{

    protected $table = 'rl_members';

    public function getAvatarAttribute($val): string
    {
        return $val?:sprintf("%s/image/default.png",env('APP_URL'));
    }

    //获取用户信息通过手机哈
    public static function queryMemberByMobile(string $mobile): array
    {
        $member=MemberModel::where([
            'mobile'		    =>$mobile,
            'status'            =>CORRECT
        ])->first();
        return !empty($member)?$member->toArray():[];
    }

    //获取用户信息通过memberId
    public static function queryMemberByMemberId(int $memberId): array
    {
        $member= MemberModel::where([
            'id'                =>$memberId,
            'status'            =>CORRECT
        ])->first();
        return !empty($member)?$member->toArray():[];
    }

    //注册操作
    public static function userRegister(string $mobile): int
    {
        //注册数据
        $data=[
            'member_sn'		    =>UUIDService::getUUID(),
            'mobile'		    =>$mobile,
            'last_login_time'   =>time(),
            'create_time'       =>time()
        ];
        return MemberModel::insertGetId($data)?:0;
    }

    //更新用户登录信息通过memberId
    public static function updateMemberLoginDataByMemberId(int $memberId): int
    {
        $updateData=[
            'last_login_time'=>time(),
            'update_time'    =>time(),
        ];
        return MemberModel::where([
            'id'    =>$memberId,
            'status'=>CORRECT
        ])->update($updateData);
    }

}